﻿//We are given integer number n, value v (v=0 or 1) and a position p. 
//Write a sequence of operators that modifies n to hold the value v 
//at the position p from the binary representation of n.
//Example: n = 5 (00000101), p=3, v=1  13 (00001101)
//n = 5 (00000101), p=2, v=0  1 (00000001)


using System;

namespace ExchangeValue
{
    class Value
    {
        static void Main()
        {
            Console.Write("Please enter the number n: ");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine(n + " which in binary is:  " + Convert.ToString(n, 2));
            Console.Write("Please enter the value v [1 or 0]: ");
            int v = int.Parse(Console.ReadLine());
            Console.Write("Please enter the position p: ");
            int p = int.Parse(Console.ReadLine());

            int mask, result;
            
            if (v==0)
            {
                mask = ~(1 << p);
                result = n & mask;
            }
            else
            {
                mask = (1 << p);
                result = n | mask;
            }
            Console.WriteLine(result + " which in binary is:  " + Convert.ToString(result, 2));
        }
    }
}
